require(['Jquery','Hui','HuiAdmin','Layer'], function(jquery,hui,huiadmin,layer){

	// 处理
	_group_handle = function(title,html,url,edit_id = 0,data = null){
		layer.open({
		    type: 1,
		    title: title,
		    shade: 0.5,
		    scrollbar : false,
		    area : ['30%','40%'],
		    content: html,
		    btn: ['确认', '取消'],
		    success: function(layero,index) {
			  	if (edit_id) {
			  		$(layero).find("#group-id").val(edit_id);
			  		$(layero).find("#group-title").val(data.name);
			  		if (data.status==1) {
			  			$(layero).find("#status-1").prop("checked","checked");
			  		}else{
			  			$(layero).find("#status-2").prop("checked","checked");
			  		}
			  		if (data.isdelete==1) {
			  			$(layero).find("#isdelete-1").prop("checked","checked");
			  		}else{
			  			$(layero).find("#isdelete-2").prop("checked","checked");
			  		}
			  	}
		    },
			yes: function(index, frame){
			    var self = $(this),
			    title = $(frame).find(".group-title").val(),
			    form = $(frame).find("#add-group-form");

			    if (!title) {
			      	layer.alert("组名不能为空",{icon: 2});
			      	return;
			    }

			    var formData = form.serialize();

			    $.post(url,formData,function(json) {
			      		if (json.code) {
			      			layer.alert(json.msg,{icon: 1});
			      			pageReload(1500);
			      		} else {
			      			layer.alert(json.msg,{icon: 5});
			      		}
			    },"json");
			},
		});
	}


	// 添加
	group_add = function(){
		$('.add-btn').click(function() {

			var title = $(this).attr('data-title');
			var html = $('#group-new-page').html();			
			var url = $(this).attr('data-url');			
			_group_handle(title,html,url);
		});
	}

	// 编辑
	group_edit = function(){
		$('.edit-btn').click(function() {
			var self = $(this),
			id = self.attr("data-id"),
			name = self.attr("data-name"),
			status = self.attr("data-status"),
			isdelete = self.attr("data-isdelete");

			var data = {'name':name,'status':status,'isdelete':isdelete};

			var title = $(this).attr('data-title');
			var html = $('#group-new-page').html();	
			var url = $(this).attr('data-url');

			_group_handle(title,html,url,id,data);
		});
	}

	// 删除
	group_del = function() {
		$('.delete-btn').click(function(){
			var self = $(this),
				id   = self.attr('data-id'),
				url  = self.attr('data-url');
			layer.msg('确定要删除吗？', {
				time: 0 //不自动关闭
				,btn: ['确定', '关闭']
				,yes: function(index){
				    layer.close(index);
						var params = {id: id};
						$.post(url, params, function(json){
							if (json.code) {
								layer.alert(json.msg,{icon: 1});
								pageReload(1000);
							} else {
								layer.alert(json.msg,{icon: 5});
							}
						}, 'json');
				}
			});
		});
	};

	// 配置规则/用户
	set_auth_or_user = function() {
		$('.set-btn').click(function(){
			var set_url = $(this).attr('set-url');
			var save_url = $(this).attr('save-url');
			var title = $(this).attr('data-title');

			layer.open({
			  	type: 2,
			  	title: title,
			  	shade: 0.5,
			  	scrollbar : false,
			  	area : ['80%','80%'],
			  	content: set_url,
			  	maxmin: true,
			  	btn: ['保存','取消'],
			  	yes: function (index, frame) {
			  		var iframe = layer.getChildFrame('body', index);
			  		var form = $(iframe).find("#set");

			  		var formData = form.serialize();

			  		$.post(save_url,formData,function(json){
			  			// layer.close(index);
			  			if (json.code) {
			  				layer.alert(json.msg,{icon: 1});
			  				pageReload(500);
			  			}else{
			  				layer.alert(json.msg,{icon: 5});
			  			}
			  		},"json");
			  	}
			});
		});
	}

	// 批量处理
	batch_proc = function(){
		$('.batch-btn').click(function(){
			var self = $(this),
				url  = self.attr('data-url'),
				type  = self.attr('data-type'),
				title  = self.attr('data-title');
			var ids = get_checkbox_group_value('group_id[]');

			if (ids == false) {
				layer.alert('请选择要批量'+ title +'的用户组',{icon: 2});
				return false;
			}
// alert(ids);
			layer.alert('确定要批量'+ title +'选中用户组吗？', {
				time: 0 ,//不自动关闭,
				btn: ['确定', '关闭'],
				yes: function(index){
				    layer.close(index);
						var data = {ids: ids, type:type};
						$.post(url, data, function(json){
							if (json.code) {
								layer.alert(json.msg,{icon: 1});
								pageReload(1000);
							} else {
								layer.alert(json.msg,{icon: 5});
							}
						}, 'json');
				}
			});
		})
	}

	// 添加
	group_add();

	// 编辑
	group_edit();

	// 删除
	group_del();

	// 配置规则
	set_auth_or_user();

	// 批量处理
	batch_proc();
});